package com.eslserver.action.schedule;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.aware.ScheduleAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.ScheduleDAO;
import com.eslserver.to.Schedule;
import com.eslserver.to.wrapper.ScheduleWrapper;
import com.eslserver.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadScheduleInfo extends HttpServlet implements ScheduleAware {

	public static final Logger LOG = LoggerFactory
			.getLogger(LoadScheduleInfo.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		PrintWriter writer = resp.getWriter();
		try {
			Schedule schedule = Schedule.createSchedule();
			String scheduleID = req.getParameter(PARAM_SCHEDULE_ID);

			if (scheduleID != null && Integer.valueOf(scheduleID) != 0) {
				ScheduleDAO scheduleDAO = ESLServerDAOFactory.getDAOFactory(
						ESLServerDAOFactory.ESLSERVER_DAOFACTORY)
						.getScheduleDAO();
				schedule = scheduleDAO.loadSchedule(Integer
						.parseInt(scheduleID));
			}

			ScheduleWrapper scheduleWrapper = ScheduleWrapper
					.createScheduleWrapper(schedule);
			writer.println(JSONResponseUtil
					.getJSONObjectString(scheduleWrapper));
		} catch (DAOException e) {
			LOG.error("error laoding Schedule Info", e);
		} finally {
			writer.close();
		}
	}

}
